package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResultBak;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissonController {
    @Autowired
    private IPermissionService permissionService;

    // 处理查询所有部门请求
    @RequestMapping("/list")
    @RequirePermission(name="权限页面",expression="permission:list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {//把qo传到list.jsp的方法一,手动贴注解
        model.addAttribute("pageResult", permissionService.query(qo));
        //把qo传到list.jsp的方法二:
        //model.addAttribute("qo",qo);
        return "permission/list"; // 找视图 /WEB-INF/views/permission/list.jsp
    }
    // 处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    //前段方式重定向
    public JsonResult delete(Long id) {

        try {
            if (id != null) {
                permissionService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"系统异常,请联系管理员"); // 让浏览器重新发一次请求
        }
    }

    @RequestMapping("/reload")
    @ResponseBody
    //前段方式重定向
    public JsonResult reload() {

        try {
                permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"系统异常,请联系管理员"); // 让浏览器重新发一次请求
        }
    }
}
